package com.minglead.springboot.platform.sys.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.minglead.springboot.common.crud.BaseController;
import com.minglead.springboot.common.persistence.Page;
import com.minglead.springboot.common.utils.StringUtils;
import com.minglead.springboot.platform.sys.entity.DesktopPlug;
import com.minglead.springboot.platform.sys.entity.DesktopPlugConfig;
import com.minglead.springboot.platform.sys.entity.Role;
import com.minglead.springboot.platform.sys.service.DesktopPlugConfigService;
import com.minglead.springboot.platform.sys.service.DesktopPlugService;
import com.minglead.springboot.platform.sys.service.RoleService;
import com.minglead.springboot.platform.sys.service.SystemService;
import com.minglead.springboot.platform.sys.utils.ActionResponse;

/**
 * 桌面插件配置Controller
 * @author clz
 * @version 2017-09-05
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/desktopPlugConfig")
public class DesktopPlugConfigController extends BaseController {

	@Autowired
	private DesktopPlugConfigService desktopPlugConfigService;
	
	@Autowired
	private DesktopPlugService desktopPlugService;
	
	
	@Autowired
	private SystemService systemService;
	
	@Autowired
	private RoleService roleService;
	
	@ModelAttribute
	public DesktopPlugConfig get(@RequestParam(required=false) String id) {
		DesktopPlugConfig entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = desktopPlugConfigService.get(id);
		}
		if (entity == null){
			entity = new DesktopPlugConfig();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:desktopPlugConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(DesktopPlugConfig desktopPlugConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/sys/desktopPlugConfigList";
	}

	/**
	 * @param desktopPlugConfig
	 * @param id 角色id
	 * @param request
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:desktopPlugConfig:view")
	@RequestMapping(value = "form")
	public String form(DesktopPlugConfig desktopPlugConfig,HttpServletRequest request,Model model) {
		Role role = roleService.get(desktopPlugConfig.getRoleId());
		request.setAttribute("role", role);	
		
		//所有插件
		List<DesktopPlug> allList = desktopPlugService.findList(new DesktopPlug());
		request.setAttribute("allList", allList);	
		//未选的
		List<DesktopPlug> unSelectList = desktopPlugService.findList(new DesktopPlug());
		//已选的
		List<DesktopPlugConfig> hasSelectList = desktopPlugConfigService.getListByRoleId(role.getId());
		request.setAttribute("hasSelectList", hasSelectList);	
	
		Iterator<DesktopPlug> itor = unSelectList.iterator();
		while(itor.hasNext()){
			
			DesktopPlug d = itor.next();			
			for(DesktopPlugConfig conifg:hasSelectList){				
				if(d.getId().equals(conifg.getPlugId())){
					itor.remove();
					break;
				}
			}		
		}
		
		request.setAttribute("unSelectList", unSelectList);	
				
		request.setAttribute("hasSelectList", hasSelectList);	
		return "modules/sys/desktopPlugConfigForm";
	}

	@RequiresPermissions("sys:desktopPlugConfig:edit")
	@RequestMapping(value = "save")
	public void save(String roleId,String plugIds,HttpServletRequest request,HttpServletResponse response,Model model, RedirectAttributes redirectAttributes) {
		ActionResponse r = new ActionResponse(1,"保存成功！");
		desktopPlugConfigService.saveEntity(roleId,plugIds);
		this.responseObject(r, response);
	}
	
	@RequiresPermissions("sys:desktopPlugConfig:edit")
	@RequestMapping(value = "delete")
	public void delete(@Param("id") String id, HttpServletResponse response,RedirectAttributes redirectAttributes) {
		
		ActionResponse r = new ActionResponse(1,"操作成功！");
		if(StringUtils.isEmpty(id)){
			r.setMessage("删除失败,id不能为null");
			this.responseObject(r, response);
			return;
		}	
		String[] _id = id.split(",");
		for(String id0:_id){
			DesktopPlugConfig temp = new DesktopPlugConfig();
			temp.setId(id0);
			desktopPlugConfigService.delete(temp);
		}
		this.responseObject(r, response);
	}
	
	/**
	 * 一次性把所有角色查询出来
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequiresPermissions("sys:desktopPlugConfig:view")
	@RequestMapping(value="loadData")
	public void loadData(HttpServletRequest request, HttpServletResponse response) throws Exception{
			
        List<DesktopPlugConfigView> viewList = new ArrayList<DesktopPlugConfigView>();
		List<Role> list = systemService.findAllRole();
		for(Role entity:list){
			DesktopPlugConfigView dv = new DesktopPlugConfigView();
		
			String code = entity.getCode();
			dv.setCode(code);
			dv.setId(entity.getId());
			dv.setName(entity.getName());
			
			List<DesktopPlugConfig> desktopPlugConfigList = desktopPlugConfigService.getListByRoleId(entity.getId());
			if(desktopPlugConfigList.size()>0){
				String desc = "";
				for(DesktopPlugConfig config:desktopPlugConfigList){
					desc = desc+config.getPlugIdName()+",";
				}
				dv.setArrowPlugsDesc(desc);
			}
			viewList.add(dv);
			
			
		}
		
        Page<DesktopPlugConfigView> page = new Page<DesktopPlugConfigView>(request,response);
        page.setList(viewList);
        page.setCount(viewList.size());        
		renderGrid(request, response, page);
	}
	
	//用于模块列表展示
	
	public  class DesktopPlugConfigView{
		private String id;//角色id
		private String name;//角色名称
		private String code;		// 角色code
		private String arrowPlugsDesc;//允许访问插件描述
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public String getArrowPlugsDesc() {
			return arrowPlugsDesc;
		}
		public void setArrowPlugsDesc(String arrowPlugsDesc) {
			this.arrowPlugsDesc = arrowPlugsDesc;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
			
	}
	
}